package com.v9.common.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.v9.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

@Data
@Schema(description ="分页信息" )
public class PageInfoDto implements Serializable {
    @Schema(description = "页索引")
    @JsonProperty("pageNum")
    private Integer page ;

    /**
     * 页大小
     */
    @Schema(description = "页大小",defaultValue = "10")
    @JsonProperty("pageSize")
    private Integer size;
    /**
     * 排序列
     */
    @Schema(description = "排序",example = "create_time")
    private String orderColumn;
    /**
     * 排序类型
     */
    @Schema(description = "排序类型",defaultValue = "DESC")
    private Sort.Direction orderType = Sort.Direction.DESC;

    @JsonIgnore
    public Pageable getPageable() {
        if (page == null) {
            page = 1;
        }
        if (size == null || size.intValue() == 0) {
            size = 10;
        }
        Sort sort = Sort.unsorted();
        if (StringUtils.isBlank(orderColumn)) {
            orderColumn = "create_time";
        }
        sort = Sort.by(orderType == null ? Sort.Direction.DESC : orderType,orderColumn);
        return PageRequest.of(page, size, sort);
    }
}
